<?php
namespace app\admin\controller;
use \think\Request;
use \think\Db;
use app\common\model\DogFamily as DogFamilyModel;
use app\common\model\Area as AreaModel;
class Dog extends \app\admin\controller\Base {

    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list'),
        'statistics'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
        'column'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
    );

    public function index(){
        $DogFamilyModel = new DogFamilyModel();
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $DogFamilyModel->relData(15,$search);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[5]!=''){
            $whereCity['pid'] = $search[5];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[6]!=''){
            $whereDistrict['pid'] = $search[6];

        }else{
            $whereDistrict['pid'] = 52;

        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'udf_size'=>$DogFamilyModel::$udf_size,
                'udf_age'=>$DogFamilyModel::$udf_age,
                'udf_dog_vaccine'=>$DogFamilyModel::$udf_dog_vaccine,
                'udf_dog_outside'=>$DogFamilyModel::$udf_dog_outside,
                'udf_dog_inside'=>$DogFamilyModel::$udf_dog_inside,
                'udf_dog_sterilization'=>$DogFamilyModel::$udf_dog_sterilization,
                'udf_dog_disability'=>$DogFamilyModel::$udf_dog_disability,
                'udf_sex'=>$DogFamilyModel::$udf_sex,
                'udf_condition'=>$DogFamilyModel::$udf_condition,
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
    return $this->fetch();
    }

    public function detail(){
        $id = input('get.id');
        $DogFamilyModel = new DogFamilyModel();
        $where['udf_id'] = array('=',$id);
        $list = $DogFamilyModel->detail($where);
        if($list['user_attestation']==2||$list['user_attestation']==3){
            $list['user_attestation'] = '是';//狗的主人认证状态
        }else{
            $list['user_attestation'] = '否';//狗的主人认证状态
        }
        if($list['user_gender']==1){
            $list['user_sex'] = '男';//狗的主人性别
        }else{
            $list['user_sex'] = '女';//狗的主人性别
        }
        if($list['user_card']!=''){
            $list['user_age'] = $this->getAgeByID($list['user_card']);
            $list['address'] = $this->userCard(substr($list['user_card'],0,2));

        }else{
            $list['user_age'] = '';
            $list['address'] = '';
        }
        $this->assign(
            array(
                'udf_size'=>$DogFamilyModel::$udf_size,
                'udf_age'=>$DogFamilyModel::$udf_age,
                'udf_dog_vaccine'=>$DogFamilyModel::$udf_dog_vaccine,
                'udf_dog_outside'=>$DogFamilyModel::$udf_dog_outside,
                'udf_dog_inside'=>$DogFamilyModel::$udf_dog_inside,
                'udf_dog_sterilization'=>$DogFamilyModel::$udf_dog_sterilization,
                'udf_dog_disability'=>$DogFamilyModel::$udf_dog_disability,
                'udf_sex'=>$DogFamilyModel::$udf_sex,
                'udf_condition'=>$DogFamilyModel::$udf_condition,
                'info'=>$list,
        ));
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     */
    public function delDog(){
        $ids = strtoarray(input('post.ids'));
        $DogFamilyModel = new DogFamilyModel();
        $where['udf_id'] = array('In', $ids);
        $result = $DogFamilyModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * 查询市
     */
    public function city(){
        $city = $_POST['city'];
        $cityArray = Db::table('up_area')->where('pid',$city)->select();
        $data['cityArray'] = $cityArray;
       return json($data);
    }
    /**
     * 查询区
     */
    public function area(){
        $area = $_POST['area'];
        $cityArray = Db::table('up_area')->where('pid',$area)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }

    public function excelDownload(){
        $DogFamilyModel = new DogFamilyModel();
        $search = input('post.');

        $res = $DogFamilyModel->excelData($search);
        $result = array();
        foreach ($res as $key=>$vo){
            $result[$key]['udf_id'] = $vo['udf_id'];
            $result[$key]['udf_name'] = $vo['udf_name'];
            if($vo['udf_condition']==1){
                $result[$key]['udf_condition'] = '待救助流浪狗';
            }elseif ($vo['udf_condition']==2){
                $result[$key]['udf_condition'] = '已救助流浪狗';
            }elseif ($vo['udf_condition']==3){
                $result[$key]['udf_condition'] = '家养宠物';
            }else {
                $result[$key]['udf_condition'] = '不详';
            }
            $result[$key]['province'] = $vo['province'];//省
            $result[$key]['city'] = $vo['city'];//市
            $result[$key]['area'] = $vo['district'];//区

            if($vo['udf_sex']==1){
                $result[$key]['sex'] ='公' ;//狗的性别
            }else{
                $result[$key]['sex'] = '母';//狗的性别
            }

            if($vo['udf_age']==1){
                $result[$key]['age'] = '不满12个月';//狗的年龄
            }elseif ($vo['udf_age']==2){
                $result[$key]['age'] = '1-3岁';//狗的年龄
            }elseif ($vo['udf_age']==3){
                $result[$key]['age'] = '4-6岁';//狗的年龄
            }elseif($vo['udf_age']==4){
                $result[$key]['age'] = '7岁以上';//狗的年龄
            }else{
                $result[$key]['age'] = '不详';//狗的年龄
            }

            if($vo['udf_size']==1){
                $result[$key]['size'] = '小型犬';//狗的体型
            }elseif ($vo['udf_size']==2){
                $result[$key]['size'] = '中型犬';//狗的体型
            }else{
                $result[$key]['size'] = '大型犬';//狗的体型
            }
            if($vo['udf_dog_vaccine']==1){
                $result[$key]['vaccine'] = '没有';//狂犬疫苗
            }elseif($vo['udf_dog_vaccine']==2){
                $result[$key]['vaccine'] = '有';//狂犬疫苗
            }else{
                $result[$key]['vaccine'] = '不详';//狂犬疫苗
            }

            if($vo['udf_dog_outside']==1){
                $result[$key]['outside'] = '没有';//体外驱虫
            }elseif($vo['udf_dog_outside']==2){
                $result[$key]['outside'] = '有';//体外驱虫
            }else{
                $result[$key]['outside'] = '不详';//体外驱虫
            }

            if($vo['udf_dog_inside']==1){
                $result[$key]['inside'] = '没有';//体内驱虫
            }elseif($vo['udf_dog_inside']==2){
                $result[$key]['inside'] = '有';//体内驱虫
            }else{
                $result[$key]['inside'] = '不详';//体内驱虫
            }

            if($vo['udf_dog_sterilization']==1){
                $result[$key]['sterilization'] = '没有';//绝育
            }elseif($vo['udf_dog_sterilization']==2){
                $result[$key]['sterilization'] = '有';//绝育
            }else{
                $result[$key]['sterilization'] = '不详';//绝育
            }

            if($vo['udf_dog_disability']==1){
                $result[$key]['disability'] = '没有';//残疾
            }elseif($vo['udf_dog_disability']==2){
                $result[$key]['disability'] = '有';//残疾
            }else{
                $result[$key]['disability'] = '不详';//残疾
            }

            $result[$key]['character'] = $vo['udskca_name'];//性格
            $result[$key]['udf_story'] = $vo['udf_story'];//狗的故事
            $result[$key]['user_id'] = $vo['user_id'];//狗的故事
            if($vo['user_attestation']==2||$vo['user_attestation']==3){
                $result[$key]['user_attestation'] = '是';//狗的主人认证状态
            }else{
                $result[$key]['user_attestation'] = '否';//狗的主人认证状态
            }
            if($vo['user_gender']==1){
                $result[$key]['user_sex'] = '男';//狗的主人性别
            }else{
                $result[$key]['user_sex'] = '女';//狗的主人性别
            }
            if($vo['user_card']!=''){
                $result[$key]['user_age'] = $this->getAgeByID($vo['user_card']);
                $result[$key]['address'] = $this->userCard(substr($vo['user_card'],0,2));

            }else{
                $result[$key]['user_age'] = '';
                $result[$key]['address'] = '';
            }
            $result[$key]['equipment'] = $vo['equipment'];

            $user_time = strtotime($vo['udf_datetime']);
            $result[$key]['udf_time'] =date('Y',$user_time).'/'.date('m',$user_time).'/'.date('d',$user_time).' '.date('H', $user_time).':'.date('i', $user_time);
            if($vo['udf_updatetime']!=''){
                $user_time1 = strtotime($vo['udf_updatetime']);
                $result[$key]['udf_updatetime'] =date('Y',$user_time1).'/'.date('m',$user_time1).'/'.date('d',$user_time1).' '.date('H', $user_time1).':'.date('i', $user_time1);
            }else{
                $result[$key]['udf_updatetime']='';
            }
        }
        $xlsCell  = array(
            array('udf_id','狗狗的ID'),
            array('udf_name','狗狗的名字'),
            array('udf_condition','狗的状况'),
            array('province','省'),
            array('city','市'),
            array('area','区'),
            array('sex','性别'),
            array('age','年龄阶段'),
            array('size','体型'),
            array('vaccine','狂犬疫苗'),
            array('outside','体外驱虫'),
            array('inside','体内驱虫'),
            array('sterilization','绝育'),
            array('disability','残疾'),
            array('character','性格'),
            array('udf_story','狗的故事'),
            array('user_id','用户Id'),
            array('user_attestation','是否认证'),
            array('user_sex','主人性别'),
            array('user_age','主人年龄'),
            array('address','户口所在地'),
            array('equipment','设备'),
            array('udf_time','新建时间'),
            array('udf_updatetime','修改时间'),
        );
        $data = $this->exportExcel($xlsCell,$result);
        return $data;
    }


    /***
     * 统计
     */
    public function statistics(){
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[4]!=''){
            $whereCity['pid'] = $search[4];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[5]!=''){
            $whereDistrict['pid'] = $search[5];

        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }


    /**
     * @return \think\response\Json
     */
    public function echartsList(){
        $search = input('post.search/a');
        $DogFamilyModel = new DogFamilyModel();
//        $total = $DogFamilyModel->echarsTotal($search);//总的数据

        $sex  = $this->branch($search,'','udf_sex');//狗的性别  1:公  2：母',
        $age  = $this->branch($search,'','udf_age');//年龄1：不满 12 个月    2：1-3 岁     3：4-6 岁   4：7 岁以上   5：不详',
        $size  = $this->branch($search,'','udf_size');//体型 1：小型犬、 2：中型犬、  3：大型犬',
        $vaccine  = $this->branch($search,'','udf_dog_vaccine');//狂犬疫苗 1：否   2：是   3：不详',
        $outside  = $this->branch($search,'','udf_dog_outside'); //狗是否体外驱虫  1：否   2：是   3：不详 ',
        $inside = $this->branch($search,'','udf_dog_inside');//体内驱虫   1：否  2：是    3：不详',
        $sterilization  = $this->branch($search,'','udf_dog_sterilization');//狗是否绝育  1：否  2：是   3：不详
        $disability  = $this->branch($search,'','udf_dog_disability');//狗是否残疾   1：否  2：是   3：不详
        $totalArray = array();
        $totalNum = array();
        $totalName = array();
        foreach ($sex as $key=>$vo){
           if($vo['udf_sex']==1){
               $vo['name'] = "公:  ".$vo['value'];
           }else if($vo['udf_sex']==2){
               $vo['name'] = "母:  ".$vo['value'];
           }else if($vo['udf_sex']==3){
               $vo['name'] = "不详:  ".$vo['value'];
           }
           unset($vo['udf_sex']);
            $totalArray['sex'][] = $vo['name'];
            $totalNum['sex'][] = $vo;
            $totalName['sex']='性别';
        }

        foreach ($age as $key1=>$vo1){
            if($vo1['udf_age']==1){
                $vo1['name'] = "不满12个月:  ".$vo1['value'];
            }elseif($vo1['udf_age']==2){
                $vo1['name'] = "1-3岁:    ".$vo1['value'];
            }elseif ($vo1['udf_age']==3){
                $vo1['name'] = "4-6岁:   ".$vo1['value'];
            }elseif ($vo1['udf_age']==4){
                $vo1['name'] = "7岁以上:   ".$vo1['value'];
            }elseif($vo1['udf_age']==5){
                $vo1['name'] = "不详:   ".$vo1['value'];
            }
            unset($vo1['udf_age']);
            $totalArray['age'][] = $vo1['name'];
            $totalNum['age'][] = $vo1;
            $totalName['age'] ='年龄';
        }

        foreach ($size as $key2=>$vo2){
            if($vo2['udf_size']==1){
                $vo2['name'] = "小型犬:   ".$vo2['value'];
            }elseif($vo2['udf_size']==2){
                $vo2['name'] = "中型犬:   ".$vo2['value'];
            }elseif($vo2['udf_size']==3){
                $vo2['name'] = "大型犬:   ".$vo2['value'];
            }elseif($vo2['udf_size']==4){
                $vo2['name'] = "不详:   ".$vo2['value'];

            }

            unset($vo2['udf_size']);
            $totalArray['size'][] = $vo2['name'];
            $totalNum['size'][] = $vo2;
            $totalName['size'] ='体型';

        }

        foreach ($vaccine as $key3=>$vo3){
            if($vo3['udf_dog_vaccine']==1){
                $vo3['name'] = "否:".$vo3['value'];
            }elseif($vo3['udf_dog_vaccine']==2){
                $vo3['name'] = "是:    ".$vo3['value'];
            }elseif($vo3['udf_dog_vaccine']==3){
                $vo3['name'] = "不详:   ".$vo3['value'];
            }
            unset($vo3['udf_dog_vaccine']);
            $totalArray['vaccine'][] = $vo3['name'];
            $totalNum['vaccine'][] = $vo3;
            $totalName['vaccine'] ='狂犬疫苗';

        }

        foreach ($outside as $key4=>$vo4){
            if($vo4['udf_dog_outside']==1){
                $vo4['name'] = "否:   ".$vo4['value'];
            }elseif($vo4['udf_dog_outside']==2){
                $vo4['name'] = "是:   ".$vo4['value'];
            }elseif($vo4['udf_dog_outside']==3){
                $vo4['name'] = "不详:  ".$vo4['value'];
            }
            unset($vo4['udf_dog_outside']);
            $totalArray['outside'][] = $vo4['name'];
            $totalNum['outside'][] = $vo4;
            $totalName['outside'] ='体外驱虫';

        }


        foreach ($inside as $key5=>$vo5){
            if($vo5['udf_dog_inside']==1){
                $vo5['name'] = "否:   ".$vo5['value'];
            }elseif($vo5['udf_dog_inside']==2){
                $vo5['name'] = "是:   ".$vo5['value'];
            }elseif($vo5['udf_dog_inside']==3){
                $vo5['name'] = "不详:  ".$vo5['value'];
            }
            unset($vo5['udf_dog_inside']);
            $totalArray['inside'][] = $vo5['name'];
            $totalNum['inside'][] = $vo5;
            $totalName['inside'] ='体内驱虫';

        }

        foreach ($sterilization as $key6=>$vo6){
            if($vo6['udf_dog_sterilization']==1){
                $vo6['name'] = "否:   ".$vo6['value'];
            }elseif($vo6['udf_dog_sterilization']==2){
                $vo6['name'] = "是:   ".$vo6['value'];
            }elseif($vo6['udf_dog_sterilization']==3){
                $vo6['name'] = "不详:  ".$vo6['value'];
            }
            unset($vo6['udf_dog_sterilization']);
            $totalArray['sterilization'][] = $vo6['name'];
            $totalNum['sterilization'][] = $vo6;
            $totalName['sterilization'] ='绝育';

        }

        foreach ($disability as $key7=>$vo7){
            if($vo7['udf_dog_disability']==1){
                $vo7['name'] = "否:   ".$vo7['value'];
            }elseif($vo7['udf_dog_disability']==2){
                $vo7['name'] = "是:   ".$vo7['value'];
            }elseif($vo7['udf_dog_disability']==3){
                $vo7['name'] = "不详:  ".$vo7['value'];
            }
            unset($vo7['udf_dog_disability']);
            $totalArray['disability'][] = $vo7['name'];
            $totalNum['disability'][] = $vo7;
            $totalName['disability'] ='残疾';

        }

        $data = array('totalArray'=>$totalArray,'totalNum'=>$totalNum,'status'=>200,'totalName'=>$totalName);
        return json($data);
    }

    /**
     * @return mixed
     * 柱状图表
     */
    public function column(){
        $AreaModel = new AreaModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[4]!=''){
            $whereCity['pid'] = $search[4];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[5]!=''){
            $whereDistrict['pid'] = $search[5];

        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'page'=>$page,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 柱状图
     */
    public function echartsColumn(){
        $search = input('post.search/a');
        $DogFamilyModel = new DogFamilyModel();
        if($search[3]!=''&&$search[4]!=''){
            $date_time = $this->prDates($search[3],$search[4],$search[5]);
        }else{
            $date_time = $this->prDates();
        }

        $count = count($date_time);
        if($search[5]==''){//默认年月日
            $start = $date_time[0];
            $end = $date_time[count($date_time)-1];
            $whereMap['u.udf_time'] = array('between',array($start,$end));
        }else{//证明按了点击按钮
            $whereMap = $this->dateDogArray($search,$count,$date_time);
        }
        $udf_condition1 = $DogFamilyModel->columnDog($search,$whereMap,1);//待救助流浪狗
        $udf_condition2 = $DogFamilyModel->columnDog($search,$whereMap,2);//已救助流浪狗
        $udf_condition3 = $DogFamilyModel->columnDog($search,$whereMap,3);//家养宠物
        $udf_condition4 = $DogFamilyModel->columnDog($search,$whereMap,'');//总的狗狗
        if($udf_condition4!=0){
            $proposer_status1 =number_format($udf_condition1/$udf_condition4,2)*100;//待救助流浪狗
            $proposer_status2 =number_format($udf_condition2/$udf_condition4,2)*100;//已救助流浪狗
            $proposer_status3 =number_format($udf_condition3/$udf_condition4,2)*100;//家养宠物
        }else{
            $proposer_status1 = 0;
            $proposer_status2 = 0;
            $proposer_status3 = 0;
        }
        $result['proposer_status1'] =$proposer_status1?$proposer_status1:0;
        $result['udf_condition1'] =$udf_condition1?$udf_condition1:0;
        $result['proposer_status2'] =$proposer_status2?$proposer_status2:0;
        $result['udf_condition2'] =$udf_condition2?$udf_condition2:0;
        $result['proposer_status3'] =$proposer_status3?$proposer_status3:0;
        $result['udf_condition3'] =$udf_condition3?$udf_condition3:0;
        foreach ($date_time as $key=>$vo){
            $result['time'][]=$vo;
            if($search[5]==1){
                $whereHe['u.udf_time'] = array('elt',$vo);
            }else if($search[5]==2){
                $whereHe['u.udf_month'] = array('elt',$vo);
            }else if($search[5]==3){
                $whereHe['u.udf_year'] = array('elt',$vo);
            }else{
                $whereHe['u.udf_time'] = array('elt',$vo);
            }
            if($search[5]==1){
                $whereHe1['u.udf_time'] = array('EQ',$vo);
            }else if($search[5]==2){
                $whereHe1['u.udf_month'] = array('EQ',$vo);
            }else if($search[5]==3){
                $whereHe1['u.udf_year'] = array('EQ',$vo);
            }else{
                $whereHe1['u.udf_time'] = array('EQ',$vo);
            }
            $value  = $DogFamilyModel->columnDog($search,$whereHe,'');
            $value1  = $DogFamilyModel->columnDog($search,$whereHe1,'');
            $result['value'][]=$value?$value:0;
            $result['value1'][]=$value1?$value1:0;
        }
        $data = array('totalArray'=>$result,'status'=>200);
        return json($data);
    }



    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询每个的数据
     */
    public function branch($search,$whereS,$type){
        $where = '';
        if($search[0]!=''){
            $where['u.udf_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.udf_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.udf_district'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-1 month"));
            $where['u.udf_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.udf_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.udf_time'] = array('egt',$search[3]);
        }else if($search[4]> 0){
            $where['u.udf_time'] = array('elt',$search[4]);
        }else{
            $where['u.udf_time'] = array('egt',0);
        }
        $where['u.'.$type] = array('neq',' ');
        $branch = Db::table('up_dog_family')->alias('u')
            ->group('u.'.$type)
            ->where($where)
            ->where($whereS)
            ->field('count(udf_id) as value,'.$type)->select();
        //总的数据
        return $branch;
    }
}